package com.system.borrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.system.borrow.Constant.URLDefind;
import com.system.borrow.MyApplication;
import com.system.borrow.R;
import com.system.borrow.bean.DetailBean;
import com.system.borrow.bean.GloData;
import com.system.borrow.cache.CacheMode;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.model.HttpParams;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.utils.SecondTitle;
import com.system.borrow.widget.CircleImageView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Request;
import okhttp3.Response;

public class DetailActivity extends BaseActivity {
    int id = 0;
    ScrollView sv;
    DetailBean detailBean;
    CircleImageView iv_img;
    TextView tv_name,tv_applyCount,tv_monthRate,tv_amount,tv_deadline,tv_conditionDesc,tv_auditDesc,tv_description;
    Button btn_subimt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        SecondTitle title = new SecondTitle(this);
        title.setTitle("产品详情",null);
        Intent intent = getIntent();
        id = intent.getIntExtra("id",0);
        initView();
        loadData(id);
        addLook();
        initListener();
    }

    private void initListener() {
        btn_subimt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(GloData.getCustomerDTO()==null){
                    Intent loginIntent = new Intent(DetailActivity.this,LoginActivity.class);
                    startActivity(loginIntent);
                    return;
                }
                Intent intent = new Intent(DetailActivity.this,WebViewActivity.class);
                intent.putExtra("name",detailBean.name);
                intent.putExtra("url",detailBean.url);
                startActivity(intent);
        }
        });
    }

    private void addLook() {
        HttpParams params = new HttpParams();
        params.put("dataId",id+"");
        params.put("flag","1");
        OkHttpUtils.post(URLDefind.ADD_LLOK)
                .tag(this)
                .params(params)
                .execute(new DialogCallback<String>(this,String.class,null) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                    }
                });

    }

    private void initView() {
        sv = (ScrollView) findViewById(R.id.sv);
        iv_img = (CircleImageView) findViewById(R.id.iv_img);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_applyCount = (TextView) findViewById(R.id.tv_applyCount);
        tv_monthRate = (TextView) findViewById(R.id.tv_monthRate);
        tv_amount = (TextView) findViewById(R.id.tv_amount);
        tv_deadline = (TextView) findViewById(R.id.tv_deadline);
        tv_conditionDesc = (TextView) findViewById(R.id.tv_conditionDesc);
        tv_auditDesc = (TextView) findViewById(R.id.tv_auditDesc);
        tv_description = (TextView) findViewById(R.id.tv_description);
        btn_subimt = (Button) findViewById(R.id.btn_subimt);
    }

    private void loadData(int id) {
        OkHttpUtils.get(URLDefind.LOAN_DETAIL+id)
                .tag(this)
                .cacheMode(CacheMode.NO_CACHE)
                .execute(new DialogCallback<String>(this,String.class,"加载中...") {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            int statusCode = jsonObject.getInt("statusCode");
                            Gson gson = new Gson();
                            if(statusCode ==200){
                                sv.setVisibility(View.VISIBLE);
                                String data = jsonObject.getString("data");
                                TypeToken<DetailBean> token = new TypeToken<DetailBean>(){};
                                detailBean = gson.fromJson(data,token.getType());
                                updataView(detailBean);
                            }else{
                                ToastShow(jsonObject.getString("message"));
                            }

                        }catch (JSONException e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                    }
                });
    }

    private void updataView(DetailBean detailBean) {
        this.detailBean = detailBean;
        MyApplication.imageLoader.displayImage(detailBean.img,iv_img);
        tv_name.setText(detailBean.name);
        tv_applyCount.setText("申请人数:"+detailBean.applyCount+"人");
        tv_monthRate.setText("利率范围:"+detailBean.monthRate);
        tv_amount.setText("贷款范围:"+detailBean.amount);
        tv_deadline.setText("期限范围:"+detailBean.deadline+"个月");
        tv_conditionDesc.setText(detailBean.conditionDesc);
        tv_auditDesc.setText(detailBean.auditDesc);
        tv_description.setText(detailBean.description);
    }
}
